var QF = {
  get: function (url, query, callback) {
    if (typeof query === "function") {
      callback = query;
      query = {};
    }
    // 请将对象 {a:1, b: 2, c: 3, d: 4} => "a=1&b=2&c=3&d=4"
    var str = '';
    for (var i in query) {
      str += i + '=' + query[i] + "&";
    }
    str = str.slice(0, -1);
    var xhr = new XMLHttpRequest();
    xhr.open("get", url + "?" + str, true);
    xhr.send();
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        var obj = JSON.parse(xhr.responseText)
        callback(obj)
      }
    }
  },
  post: function (url, query, callback) {
    // 请将对象 {a:1, b: 2, c: 3, d: 4} => "a=1&b=2&c=3&d=4"
    var str = '';
    for (var i in query) {
      str += i + '=' + query[i] + "&";
    }
    str = str.slice(0, -1);
    var xhr = new XMLHttpRequest();
    xhr.open("post", url, true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send(str);
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        var obj = JSON.parse(xhr.responseText)
        callback(obj)
      }
    }
  },
  jsonp: function (url, query, callback) {
    var str = '';
    for (var i in query) {
      str += i + '=' + query[i] + "&";
    }
    str = str.slice(0, -1);

    // 给widnow添加一个属性   widnow.good = function() {}   
    window[query.callbackname] = callback;

    // 创建script标签
    var script = document.createElement("script");
    // 设置src
    script.src = url + "?" + str;
    // 上树 才能发送请求
    document.body.appendChild(script)
    // 等执行完了 删除元素 删除属性
    script.onload = function () {
      document.body.removeChild(this);
      delete window[query.callbackname];
    }
  },
  getCookie: function (name) {
    var arr = document.cookie.split("; ");
    for (var i = 0; i < arr.length; i++) {
      var subArr = arr[i].split("=");
      if (subArr[0] === name) {
        return subArr[1];
      }
    }
  },
  setCookie: function(name, value, maxAge, path) {
    document.cookie = `${name}=${value};max-age=${maxAge}; path=${path}`
  },
  delCookie: function(name, path) {
    // cookie的删除仅仅是将指定的cookie设置为过期即可
    document.cookie=`${name}=sasdf;max-age=-1;path=${path}`;
  }
}